home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10154 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: plato.ansa.co.uk!news
  2. From: tms@ansa.co.uk (Toby Speight)
  3. Newsgroups: comp.lang.c++
  4. Subject: Stream operators (was Re: well-chosen overloading)
  5. Date: 6 Mar 1996 12:53:00 GMT
  6. Organization: ANSA, Cambridge, England.
  7. Sender: tms@socrates.ansa.co.uk
  8. Message-ID: <s8spfmv13j.fsf_-_@socrates.ansa.co.uk>
  9. References: <313613B2.136E@ksopk.sprint.com>
  10.     <4h554t$9jk@netline-fddi.jpl.nasa.gov> <4h8ka3$4o8@atlas.tus.ssi1.com>
  11.     <4hibv9$8bu@mcrware.microware.com>
  12. NNTP-Posting-Host: socrates.ansa.co.uk
  13. Mime-Version: 1.0
  14. Content-Type: text/plain; charset=US-ASCII
  15. Content-Transfer-Encoding: 7bit
  16. In-reply-to: jejones@microware.com's message of 5 Mar 1996 21:33:29 GMT
  17. X-Phone: +44 1223 568925
  18. X-Fax: +44 1223 359779
  19. X-Yow: As President I have to go vacuum my coin collection!
  20. X-Newsreader: Gnus v5.1
  21.  
  22. -----BEGIN PGP SIGNED MESSAGE-----
  23.  
  24. I'll take your points in reverse order (you'll see why)
  25.  
  26. >>>>> In article <4hibv9$8bu@mcrware.microware.com>,
  27. >>>>> James == "James Jones" <jejones@microware.com> writes:
  28.  
  29. James> (Come to think of it, while I'm asking about streams, it's
  30. James> obviously a a good idea to write
  31.  
  32. James>     cout << (i << j);
  33.  
  34. James> to output the result of shifting i left j bits, but is it
  35. James> necessary?)
  36.  
  37. Absolutely. "cout << i << j" means "(cout << i) << j"; in other words
  38. "output i, then with the resulting stream, output j".  For the
  39. standard streams, the result is the same as the original stream, so
  40. "cout << i << j" is the same as "cout << i, cout << j"
  41.  
  42. This was one of the main reasons for the stream "shift" syntax.
  43.  
  44.  
  45. James> I've always wondered why the stream output operator isn't +=
  46. James> (-= for input).
  47.  
  48. Because the precedence of += is much lower (it doesn't bind as
  49. tightly), and because it has the opposite associativity
  50. (right-to-left, rather than left-to-right).  
  51.  
  52. "cout << i << j" would become "(cout += i) += j"
  53.  
  54. which is (arguably) less readable.
  55.  
  56. See D&E, section 8.3.1 for the rationale on stream operators.
  57. -----BEGIN PGP SIGNATURE-----
  58. Version: 2.6.2i
  59. Comment: Processed by Mailcrypt 3.4, an Emacs/PGP interface
  60.  
  61. iQB1AwUBMT2Jq+dsuUurvcRtAQGJ8AL/V8K2tU5CRGS15MDPjbuk2mvJYbiOg5Lw
  62. KClxmbTTFfny+6YGlVDJDheO4vX36g12D24nRgbzQVNQCTUE4YXSdaMmWKmadI3O
  63. UF8pw9FxO5kTJ74RSkCq9Fms76ZQN6R7
  64. =B+kP
  65. -----END PGP SIGNATURE-----
  66. -- 
  67. Toby Speight <tms@ansa.co.uk>    Phone +44 1223 568925, Fax +44 1223 359779
  68. APM Ltd., Poseidon House, Castle Park, Cambridge CB3 0RD
  69.